<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body {
            height: 2000px;
        }

        .btn {
            width: 100px;
            height: 100px;
            background-color: rgb(105, 68, 68);
            text-align: center;
            line-height: 100px;
            color: aliceblue;
            position: fixed;
            bottom: 100px;
            right: 0;
        }
    </style>
</head>

<body>
    <div>这是顶部</div>
    <div class="btn">返回顶部</div>

    <script>

        const btn = document.querySelector('.btn')

        // 给window绑定滚动事件
        window.onscroll = function () {
            // 检测document距离顶部的距离
            const juli = document.documentElement.scrollTop
            // 大于600时显示,否则隐藏
            if (juli > 600) {
                btn.style.display = 'block'
            } else {
                btn.style.display = 'none'
            }
        }
        
        // 给btn绑定点击事件,然后将顶部距离赋值为0
        btn.onclick = function(){
            document.documentElement.scrollTop = 0
        }

    </script>
</body>

</html>